<?php

#################################################################
#								HTML
#################################################################

/**
 * Title element register as 'media/youtube/player' 
 * 
 * @uses $vars;
 * 
 * Defaults:
 * 'width' => 420,
 * 'height' => 315, 
 * 'hl' => 'es_ES',
 * 'version' => '3',
 * 'modestbranding' => '1',
 * 'theme' => 'light', //dark
 * 'color' => 'white', //red
 * 'autohide' => '1',
 * 'rel' => '0',
 * 'color1' => '0xCCCCCC',
 * 'color2' => '0x3B5998',
 * 'border' => '0',
 * 'fs' => '1',
 * 'autoplay' => '0',
 * 'loop' => '0',
 * 'disablekb' => '0',
 * 'egm' => '0',
 * 'start' => '0',
 * 'hd' => '0',
 * 'showsearch' => '1',
 * 'showinfo' => '1',
 * 'iv_load_policy' => '0'
 * 
 * $vars['v']		Youtube video ID
 * $vars['layout']	Youtube player layout
 * $vars['class']
 * $vars['id']
 * 
 * @author Pablo
 * @since 1.0
 */

if($_CONTEXT=='HTML'){
	
	$_CSS_STACK->push(blg_get_site_url().'media/youtube/player.css');
	$_JS_STACK->push(blg_get_site_url().'media/youtube/player.js');	
		
	//PREPARE DEFAULTS 
	$defaults= array(	
		'hl' => 'es_ES',
		'version' => '3',
		'modestbranding' => '1',
		'theme' => 'light', //dark
		'color' => 'white', //red
		'autohide' => '1',
		'rel' => '0',
		'color1' => '0xCCCCCC',
		'color2' => '0x3B5998',
		'border' => '0',
		'fs' => '1',
		'autoplay' => '0',
		'loop' => '0',
		'disablekb' => '0',
		'egm' => '0',
		'start' => '0',
		'hd' => '0',
		'showsearch' => '1',
		'showinfo' => '1',
		'iv_load_policy' => '0',	
	);
	
	$params= array_merge($defaults, $vars);
	
	if(empty($vars['v']))
		$vars['v']= YOUTUBE_ID_NOT_FOUND;
	
	try{
		$yt= new BlgYoutube($vars['v']);
	}catch(Zend_Gdata_App_HttpException $e){
		$vars['v']= YOUTUBE_ID_NOT_FOUND;
		$yt= new BlgYoutube($vars['v']);
	}

	if(empty($vars['layout']))
		$vars['layout']= 'default';	
	
	if(!v_is_numeric($vars['width']) || $vars['width']<=0 )
		$vars['width']= 420;
		
	if(!v_is_numeric($vars['height']) || $vars['height']<=0 )
		$vars['height']= 315;
		
	$class= 'youtube_player';
	if(!empty($vars['class']))
		$vars['class']= $class.' '.$vars['class'];
	else
		$vars['class']= $class;

	if(empty($vars['id']))
		$vars['id']= 'youtube_player_'.random_string(12);		
		
	// BUILD FLASH URL	
	$query= array();
	foreach($params as $param => $value){
		if(array_key_exists($param, $defaults))
			$query[]= $param.'='.$value;
	}
	$query= implode('&', $query);
	$flash_url2 = 'http://www.youtube.com/v/'.$vars['v'].'?'.$query;	
	
	$flash_url= $yt->get_flash_url();
	//pre($yt->get_flash_url());
	
	// BUILD EMBED OBJECT
	$o_embed= 'AAAA
	<object id="player_'.$vars[id].'" width="'.$vars['width'].'" height="'.$vars['height'].'">
		<param name="movie" value="'.$flash_url.'"></param>
		<param name="allowFullScreen" value="true"></param>
		<embed src="'.$flash_url.'"
		type="application/x-shockwave-flash"
		width="'.$vars['width'].'" height="'.$vars['height'].'" 
		wmode="transparent" allowfullscreen="true"></embed>
	</object>AAAA
	';	
	
	$o_embed2= 'AAAA 
	<object id="player_'.$vars[id].'" width="'.$vars['width'].'" height="'.$vars['height'].'">
		<param name="movie" value="'.$flash_url2.'"></param>
		<param name="allowFullScreen" value="true"></param>
		<embed src="'.$flash_url2.'"
		type="application/x-shockwave-flash"
		width="'.$vars['width'].'" height="'.$vars['height'].'" 
		wmode="transparent" allowfullscreen="true"></embed>
	</object>AAAA
	';		
		
		
	switch($vars['layout']){
		case 'preview':
			$player= '
			<img src="'.blg_youtube_thumbnail_url().'" /><br/>
			<img src="'.blg_youtube_thumbnail_url($vars['v']).'" /><br/>
			<img src="'.blg_youtube_thumbnail_url($vars['v'], 0).'" /><br/>
			<img src="'.blg_youtube_thumbnail_url($vars['v'], 1).'" /><br/>
			<img src="'.blg_youtube_thumbnail_url($vars['v'], 2).'" /><br/>
			<img src="'.blg_youtube_thumbnail_url($vars['v'], 3).'" /><br/>
			';
			break;
		case 'default':
		default:
			$player= '
			<div id="'.$vars['id'].'" class="'.$vars['class'].'">
				BBBB '.$o_embed.' '.$o_embed2.' BBBB
			</div>
			';
			break;			
	}			
	
	echo $player;
}
#################################################################
#								JS
#################################################################
elseif($_CONTEXT=='JS'){ ?>

<?php }
#################################################################
#								CSS
#################################################################
elseif($_CONTEXT=='CSS'){ ?>

div.youtube_player{
	display: inline-block;
}

<?php }
